package com.elm_springboot_s.controller;

import com.elm_springboot_s.pojo.Orders;
import com.elm_springboot_s.pojo.RespondMessage;
import com.elm_springboot_s.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
@Api(tags = "订单相关接口")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/createOrders")
    @ApiOperation("创建订单")
    public RespondMessage<Integer> createOrders(long userId, Integer businessId,
                                              Integer daId, Double orderTotal) {
        try {
            int orderId = ordersService.createOrders(userId, businessId, daId, orderTotal);
            return RespondMessage.success(orderId);
        } catch (Exception e) {
            return RespondMessage.error("创建订单失败：" + e.getMessage());
        }
    }

    @GetMapping("/getOrdersById")
    @ApiOperation("根据订单编号查询订单信息")
    public RespondMessage<Orders> getOrdersById(Integer orderId) {
        try {
            Orders orders = ordersService.getOrdersById(orderId);
            return RespondMessage.success(orders);
        } catch (Exception e) {
            return RespondMessage.error("查询订单失败：" + e.getMessage());
        }
    }

    @GetMapping("/listOrdersByUserId")
    @ApiOperation("根据用户编号查询订单信息")
    public RespondMessage<List<Orders>> listOrdersByUserId(Long userId) {
        try {
            List<Orders> orders = ordersService.listOrdersByUserId(userId);
            return RespondMessage.success(orders);
        } catch (Exception e) {
            return RespondMessage.error("查询订单失败：" + e.getMessage());
        }
    }
} 